// Copyright 2012 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package adwords.axis.v201109_1.basicoperations;

import com.google.api.ads.adwords.axis.v201109_1.cm.AdGroupAd;
import com.google.api.ads.adwords.axis.v201109_1.cm.AdGroupAdOperation;
import com.google.api.ads.adwords.axis.v201109_1.cm.AdGroupAdReturnValue;
import com.google.api.ads.adwords.axis.v201109_1.cm.AdGroupAdServiceInterface;
import com.google.api.ads.adwords.axis.v201109_1.cm.Dimensions;
import com.google.api.ads.adwords.axis.v201109_1.cm.Operator;
import com.google.api.ads.adwords.axis.v201109_1.cm.ThirdPartyRedirectAd;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.adwords.lib.factory.AdWordsServices;
import com.google.api.ads.common.lib.auth.ClientLoginTokens;


/**
 * This example adds a third party redirect ad to an ad group. To get ad groups,
 * run GetAdGroups.java.
 *
 * Credentials and properties in {@code fromFile()} are pulled from the
 * "ads.properties" file. See README for more info.
 *
 * Tags: AdGroupAdService.mutate
 *
 * @author Kevin Winter
 */
public class AddThirdPartyRedirectAd {

  public static void main(String[] args) throws Exception {
    // Get a ClientLogin AuthToken.
    String clientLoginToken = new ClientLoginTokens.Builder()
        .forApi(ClientLoginTokens.Api.ADWORDS)
        .fromFile()
        .build()
        .requestToken();

    // Construct an AdWordsSession.
    AdWordsSession session =
        new AdWordsSession.Builder().fromFile().withClientLoginToken(clientLoginToken).build();

    long adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");

    runExample(session, adGroupId);
  }

  public static void runExample(AdWordsSession session, long adGroupId) throws Exception {
    // Get the AdGroupAdService.
    AdGroupAdServiceInterface adGroupAdService =
        new AdWordsServices().get(session, AdGroupAdServiceInterface.class);

    // Create third party redirect ad.
    ThirdPartyRedirectAd thirdPartyRedirectAd = new ThirdPartyRedirectAd();
    thirdPartyRedirectAd.setName("Example third party ad #" + System.currentTimeMillis());
    thirdPartyRedirectAd.setUrl("http://www.example.com");
    thirdPartyRedirectAd.setDimensions(new Dimensions(300, 250));
    // This field normally contains the javascript ad tag.
    thirdPartyRedirectAd
        .setSnippet("<img src=\"https://sandbox.google.com/sandboximages/image.jpg\"/>");
    thirdPartyRedirectAd.setImpressionBeaconUrl("http://www.examples.com/beacon");
    // Image & Flash format id.
    thirdPartyRedirectAd.setCertifiedVendorFormatId(119L);
    thirdPartyRedirectAd.setIsCookieTargeted(false);
    thirdPartyRedirectAd.setIsUserInterestTargeted(false);
    thirdPartyRedirectAd.setIsTagged(false);

    // Create ad group ad.
    AdGroupAd adGroupAd = new AdGroupAd();
    adGroupAd.setAdGroupId(adGroupId);
    adGroupAd.setAd(thirdPartyRedirectAd);

    // Create operations.
    AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation();
    adGroupAdOperation.setOperand(adGroupAd);
    adGroupAdOperation.setOperator(Operator.ADD);

    AdGroupAdOperation[] operations = new AdGroupAdOperation[] {adGroupAdOperation};

    // Add ads.
    AdGroupAdReturnValue result = adGroupAdService.mutate(operations);

    // Display ads.
    for (AdGroupAd adGroupAdResult : result.getValue()) {
      System.out.println("Ad with id  \"" + adGroupAdResult.getAd().getId() + "\"" + " and type \""
          + adGroupAdResult.getAd().getAdType() + "\" was added.");
    }
  }
}
